home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / qbnws104.zip / IQUEUE.ZIP / IQUEUE.ASM next >
Assembly Source File  |  1990-05-21  |  4KB  |  116 lines

  1.         page ,132
  2.         title   IQUEUE - Add an integer FIFO queue to BC/QB
  3.         subttl  Copyright 1990, Editing Services Co.
  4.         comment |
  5.  
  6. Implements a FIFO stack (circular queue, ring buffer) by adding five
  7. procedures to QB/BC: two SUBs and three FUNCTIONs.
  8. See ISTACK for a LIFO stack.
  9.  
  10.         DECLARE SUB      IQput (intval%) 'put an item in the queue
  11.         DECLARE FUNCTION IQget% ()       'get an item from the queue
  12.         DECLARE FUNCTION IQfree% ()      'TRUE if any room in queue
  13.         DECLARE FUNCTION IQavail% ()     'TRUE if anything in queue
  14.         DECLARE SUB      IQflush ()      'clear (empty) the queue
  15.         |
  16.                                                                         page +
  17.         .model medium, basic
  18.         .data
  19.  
  20. QPutPtr dw      0               ; pointer used for storing
  21. QGetPtr dw      0               ; pointer used for retrieving
  22.  
  23.         .code
  24.  
  25. Qsize   equ     256             ; size of buffer (one fewer available)
  26.  
  27. IQueue  dw      Qsize dup (0)
  28.                                                                         page +
  29. Querr:: INT     4               ; report "Overflow" to BASIC
  30.  
  31. IQput           PROC ival
  32.  
  33. ; Accepts an integer value and places it into the queue.
  34. ; Generates an Overflow Error in QB if there is no room in the queue.
  35.  
  36.         mov     bx, ival        ; get pointer to passed value
  37.         mov     ax, [bx]        ; get actual value
  38.         mov     bx, QPutPtr     ; get current head
  39.         inc     bx              ; increment it first
  40.         cmp     bx, Qsize       ; see if off end
  41.         jb      @f              ; no, still OK
  42.         xor     bx, bx          ; yes, circulate the pointer
  43. @@:     cmp     bx, QGetPtr     ; did we run into the tail?
  44.         je      querr           ; yes: the queue is FULL
  45.         mov     QPutPtr, bx     ; otherwise, save new head
  46.         shl     bx, 1           ; turn head into a word pointer
  47.         mov     IQueue[bx], ax  ; place item into queue
  48.         ret
  49.  
  50. IQput           ENDP
  51.                                                                         page +
  52. IQget           PROC
  53.  
  54. ; Removes the oldest data item from the queue.
  55. ; Generates an Overflow Error if there are no values in the queue.
  56.  
  57.         mov     bx, QGetPtr     ; get current tail
  58.         cmp     bx, QPutPtr     ; is it at the head?
  59.         jz      querr           ; queue is empty: Overflow error
  60.         inc     bx              ; bump the tail pointer
  61.         cmp     bx, Qsize       ; off end?
  62.         jb      @f              ; no, continue
  63.         xor     bx, bx          ; yes, circulate 
  64. @@:     mov     QGetPtr, bx
  65.         shl     bx, 1           ; turn QGetPtr into a word pointer
  66.         mov     ax, IQueue[bx]  ; retrieve value from stack
  67.         shr     bx, 1           ; element number again
  68.         cmp     bx, QPutPtr     ; did we bump into the head?
  69.         jne     @f              ; no, still data left
  70.         mov     QPutPtr, 0      ; yes, out of data now...
  71.         mov     QGetPtr, 0      ; reset the pointers
  72. @@:     ret                     ; return retrieved value
  73.  
  74. IQget           ENDP
  75.                                                                         page +
  76. IQflush         PROC
  77.  
  78. ; Empty the queue by setting the head & tail equal
  79.  
  80.         mov     QPutPtr, 0
  81.         mov     QGetPtr, 0
  82.         ret
  83.  
  84. IQflush         ENDP
  85.  
  86. IQfree          PROC
  87.  
  88. ; Returns TRUE if there is any space left in the queue.
  89.  
  90.         mov     ax, QPutPtr
  91.         inc     ax
  92.         cmp     ax, Qsize
  93.         jb      @f
  94.         xor     ax, ax
  95. @@:     sub     ax, QGetPtr
  96.         jz      @f
  97.         mov     ax, -1
  98. @@:     ret
  99.  
  100. IQfree          ENDP
  101.  
  102. IQavail         PROC
  103.  
  104. ; Returns TRUE if there are any data items waiting in the queue.
  105.  
  106.         mov     ax, QGetPtr
  107.         sub     ax, QPutPtr
  108.         jz      @f
  109.         mov     ax, -1
  110. @@:     ret
  111.  
  112. IQavail         ENDP
  113.                                                                         page +
  114.         end
  115.  
  116.